import java.util.HashSet;
import java.util.Set;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/16 14:39
 * @description:
 */
public class LCR120 {
    public int findRepeatDocument(int[] documents) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < documents.length; i++) {
            if (!set.contains(documents[i])) {
                set.add(documents[i]);
            } else {
                return documents[i];
            }
        }
        return -1;
    }

    public int findRepeatDocument2(int[] documents) {
        for (int i = 0; i < documents.length; i++) {
            documents[i] = documents[i] + 1;
        }
        for (int document : documents) {
            int index = Math.abs(document) - 1;
            if (documents[index] > 0) {
                documents[index] *= -1;
            } else {
                return Math.abs(document) - 1;
            }
        }
        return -1;
    }
    public int findRepeatDocument3(int[] documents) {
        for (int i = 0; i < documents.length; i++) {
            int index = documents[i];
            if(documents[i]==i){
                continue;
            }
            if(documents[index]==documents[i]){
                return documents[i];
            }else{
                //交换索引位i和document[i]的元素值
                int temp = documents[i];
                documents[i] = documents[index];
                documents[index]=temp;
                i--;//关键交换后指针不变继续判断
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        LCR120 lcr = new LCR120();
        System.out.println(lcr.findRepeatDocument3(new int[]{2, 5, 3, 0, 5, 0}));
    }
}
